package com.smzd.framework.system.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.smzd.core.dao.AbstractDao;
import com.smzd.core.domain.Many2Many;
import com.smzd.core.mappers.IMapper;
import com.smzd.framework.system.domain.Function;
import com.smzd.framework.system.domain.Module;
import com.smzd.framework.system.mappers.FunctionMapper;
import com.smzd.framework.utils.db.RPage;
import com.smzd.framework.utils.db.SRPage;

@Component
public class FunctionDao extends AbstractDao<Function> {
    @Autowired
    private FunctionMapper mapper;

    @Override
    protected IMapper<Function> getMapper() {
        return mapper;
    }

    public RPage<Function> getRoleFunctionsListByPage(SRPage<Module, Function> page) {
        preparePage(page);
        List<Function> elems = mapper.getRoleFunctionListByPage(page);
        page.setElems(elems);
        int total = mapper.getElemsCount(page);
        page.setTotalCount(total);
        completePage(page);
        return page;
    }

    public void saveRoleFunctions(List<Many2Many> toAdd, List<Many2Many> toDel) {
        for (Many2Many add : toAdd) {
            mapper.insertRoleFunction(add);
        }
        for (Many2Many del : toDel) {
            mapper.removeRoleFunction(del);
        }
    }
}
